# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import copy

required_conan_version = ">=2.13.0"


class AbseilConan(ConanFile):
    name = "abseil"
    description = "Abseil Common Libraries (C++) from Google"
    license = "Apache-2.0"
    settings = "os", "arch", "compiler", "build_type"
    exports_sources = "permissions.ini"
    _cmake = None
    
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
        "build_tests": [True, False]
    }
    default_options = {
        "shared": False,
        "fPIC": True,
        "build_tests": False
    }

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")

    def layout(self):
        cmake_layout(self)

    def source(self):
        git = Git(self)
        git.fetch_commit(url=self.conan_data["sources"][self.version]["url"], commit=self.conan_data["sources"][self.version]["branch"])

    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["ABSL_ENABLE_INSTALL"] = "ON"
        tc.variables["BUILD_SHARED_LIBS"] = "ON" if self.options.shared else "OFF"
        tc.variables["BUILD_TESTING"] = "ON" if self.options.build_tests else "OFF"
        tc.variables["CMAKE_CXX_STANDARD"] = "17"
        tc.variables["ABSL_PROPAGATE_CXX_STD"] = "ON"
        
        # 添加编译标志
        cxxflags = []
        if self.settings.arch == "armv8":
            cxxflags.append("-march=armv8-a+crypto")
        
        tc.extra_cxxflags = cxxflags
        tc.extra_cflags = cxxflags
        tc.generate()

    def build(self):
        cmake = self._configure_cmake()
        cmake.build()

    def package(self):
        cmake = self._configure_cmake()
        cmake.install()
        copy(self, "permissions.ini", 
             src=self.export_sources_folder, 
             dst=self.package_folder)

    def package_info(self):
        self.cpp_info.includedirs = ["include"]
        self.cpp_info.libs = [
            "absl_base",
            "absl_spinlock_wait",
            "absl_malloc_internal",
            "absl_dynamic_annotations",
            "absl_throw_delegate",
            "absl_stacktrace",
        ]
        if self.settings.os == "Windows" and not self.options.shared:
            self.cpp_info.defines.append("ABSL_CONSUME_DLL")

    def _configure_cmake(self):
        if self._cmake:
            return self._cmake
        self._cmake = CMake(self)
        self._cmake.configure(cli_args=["--no-warn-unused-cli"])
        return self._cmake